//
//  WBHomeViewController.swift
//  WCKweibo
//
//  Created by wukai on 17/2/25.
//  Copyright © 2017年 wuchunkai. All rights reserved.
//

import UIKit

//定义全局常量，尽量是有private 修饰，否则到处可以访问

private let cellId = "cellId"


class WBHomeViewController: WBBaseViewController {

    lazy var statusList = [String]()
    
    override func loadData() {

        //用网络工具加载微博数据
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let params = ["access_token": "2.00scPBSD0g2tEb8609c54103TsVusD"]
        WBNetWorkManger.shared.get(urlString, parameters: params, progress: nil, success: { (_, json) in
            
            print(json as Any)
            
        })  { (_, error) in
            
            print(error)
                
            }
        
        
        
        //模拟延时加载数据 - 》 dispath_after
        let delay = DispatchTime.now() + 2
        DispatchQueue.main.asyncAfter(deadline: delay) {
            // 延迟执行
        for i in 0..<15 {
            if self.isPullup{
                //将数据插入到数组的底部
              self.statusList.append("上拉\(i)")
                
            }else{
            
                //将数据插入到数组的顶部
                self.statusList.insert(i.description, at: 0)

            }
                
                
          
            }
            
            //结束刷新
            self.refreshControl?.endRefreshing()
            
            self.isPullup = false
            
            //刷新表格
            self.tableView?.reloadData()
        }
    }
    
    //显示好友
    @objc  func showFriends(){
    
        print(#function)
    
    }

}

// MARK: - 表格数据源方法 
extension WBHomeViewController{

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print(statusList.count)
        return statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //1.取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        
        //2.设置cell
        cell.textLabel?.text = statusList[indexPath.row]
        //3.返回cell
        return cell
    }

}



// MARK: - 设置届面
extension WBHomeViewController{

    override func setupTablView() {
  
    super.setupTablView()
    
    //设置导航栏的按钮
    navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
    
    //注册原型的cell
    tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }
}
